package com.rockxin.cat.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rockxin.cat.R;
import com.rockxin.cat.adapter.RecommendAwardAdapter;
import com.rockxin.cat.base.BaseFragment;
import com.rockxin.cat.base.Constant;
import com.rockxin.cat.base.User;
import com.rockxin.cat.bean.RecommendAwardBean;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Subscriber;

/**
 * <b>类名称：    RecommendAwardFragment             <br/>
 * <b>类描述：                        <br/>
 * <b>创建人：    Lans               <br/>
 * <b>创建时间：  2018/3/4 18:52     <br/>
 * <b>修改人：                        <br/>
 * <b>修改时间：                      <br/>
 * <b>修改备注：                      <br/>
 *
 * @version 1.0.0 <br/>
 */
public class RecommendAwardFragment extends BaseFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private Unbinder bind;
    private RecommendAwardAdapter mRecommendAwardAdapter;
    private List<RecommendAwardBean> recommendAwardBeanList = new ArrayList<>();

    private int type;

    public static RecommendAwardFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        RecommendAwardFragment fragment = new RecommendAwardFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fragment_recommend_award, container, false);
        bind = ButterKnife.bind(this, mView);
        initView();
        return mView;
    }


    private void initView() {

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        recyclerView.setHasFixedSize(true);
        mRecommendAwardAdapter = new RecommendAwardAdapter(recommendAwardBeanList);
        recyclerView.setAdapter(mRecommendAwardAdapter);
        initData();


    }

    private void initData() {
        SPUtils spUtils = new SPUtils("Guide");
        String token = spUtils.getString(Constant.PREFERENCES.TOKEN);
        CatNetWork catNetWork = new CatNetWork();
        catNetWork.myInvite(type, token, 1, new Subscriber<HttpResult<List<RecommendAwardBean>>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpResult<List<RecommendAwardBean>> listHttpResult) {
                recommendAwardBeanList.addAll(listHttpResult.getData());
                mRecommendAwardAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        bind.unbind();
    }

}
